package com.csdn.springbootmybatis.controller.rest;

import com.csdn.springbootmybatis.RespStat;
import com.csdn.springbootmybatis.service.impl.RoleService;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: JLL
 * @Date: 2019/8/25 22:02
 */
@RestController
@RequestMapping("/api/v1/manager/permission")
public class RoleManagerRestController {

    @Autowired
    private RoleService roleService;

    /**
     * 给角色添加权限，角色和权限表都不用改。  改他们的关联表就可以
     *
     * 1、关键逻辑就一句话：insert into role_permission (role_id,permission_id) values (6,1)
     * 2、有个问题，重复提交的话，一个角色会重复加上相同的权限。 所有add前先delete一下。
     *
     * 3、一个xml执行多条sql要加配置：
     * url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowMultiQueries=true
     * 4、往DB发多条sql，影响性能，发一个sql就行
     *  比如：insert into role_permission (role_id,permission_id) values (6,1),(**,**),(**,**).......
     */
    @RequestMapping("Permission/add")
    public RespStat permissionadd( @RequestParam int[] permissions, @RequestParam int id ) {
        roleService.addPermission(id,permissions);
        System.out.println("permissionids:" + ToStringBuilder.reflectionToString(permissions));
        return RespStat.build(200);
    }


}
